package com.apexsoft.margin.account.management.util;

import com.apexsoft.margin.account.management.domain.common.model.AdapterResp;
import com.apexsoft.margin.account.management.domain.common.model.AdapterRespChild;
import com.apexsoft.margin.account.management.domain.search.model.FirstChainSearch;
import com.apexsoft.margin.account.management.domain.search.model.response.PreChainSearchOutputDomain;
import com.apexsoft.margin.account.management.domain.search.model.response.SecondChainSearchOutputDomain;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DealResp {
    public List<HashMap> getData(AdapterRespChild child){
        Gson gson = new Gson();
        return convertToHash(child.getResponse());

    }

    /**
     * list<T>转成的JSONString转换成list<HashMap>
     * convertObject：list<T>转换成JSONString
     * @param convertObject
     * @return list<HashMap>
     */
    public List<HashMap> convertToHash(String convertObject){
        Gson gson = new Gson();
        return gson.fromJson(convertObject,new TypeToken<List<HashMap>>(){}.getType());
    }

    public AdapterResp adapterRespBuildFirstChainSearch(List<FirstChainSearch> list){
        AdapterResp resp = new AdapterResp();
        AdapterRespChild child = new AdapterRespChild();
        if(list.size()>0){
            resp.setResultNum((short) list.size());
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            Gson gson = new Gson();
            child.setResponse(gson.toJson(list));
            child.setResultNum((short) list.size());
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }else {
            resp.setResultNum((short) 0);
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            child.setResultNum((short) 0);
            Gson gson = new Gson();
            child.setResponse("[]");
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }
        return resp;
    }

    public AdapterResp adapterRespBuildSecondSearchChain(List<SecondChainSearchOutputDomain> list){
        AdapterResp resp = new AdapterResp();
        AdapterRespChild child = new AdapterRespChild();
        if(list.size()>0){
            resp.setResultNum((short) list.size());
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            Gson gson = new Gson();
            child.setResponse(gson.toJson(list));
            child.setResultNum((short) list.size());
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }else {
            resp.setResultNum((short) 0);
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            child.setResultNum((short) 0);
            Gson gson = new Gson();
            child.setResponse("[]");
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }
        return resp;
    }

    public AdapterResp adapterRespBuildPreSearchChain(List<PreChainSearchOutputDomain> list){
        AdapterResp resp = new AdapterResp();
        AdapterRespChild child = new AdapterRespChild();
        if(list.size()>0){
            resp.setResultNum((short) list.size());
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            Gson gson = new Gson();
            child.setResponse(gson.toJson(list));
            child.setResultNum((short) list.size());
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }else {
            resp.setResultNum((short) 0);
            resp.setErrCode("");
            resp.setErrInfo("");
            resp.setFainNodes("");
            resp.setFailNum((short) 0);
            resp.setStatus((short) 1);
            child.setErrInfo("");
            child.setFunc("xxxx");
            child.setNode("4");
            child.setResultNum((short) 0);
            Gson gson = new Gson();
            child.setResponse("[]");
            Map<String,AdapterRespChild> map = new HashMap<>();
            map.put("4",child);
            resp.setResultSet(map);
        }
        return resp;
    }
}
